package com.example.spring.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.spring.demo.common.error.CustomException;
import com.example.spring.demo.entity.Goods;
import com.example.spring.demo.common.model.vo.SelectType;
import com.example.spring.demo.entity.SysBanner;
import com.example.spring.demo.common.model.vo.SysBannerParam;
import com.example.spring.demo.service.GoodsService;
import com.example.spring.demo.service.SysBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/api/banner")
@RestController
public class SysBannerController {
    @Autowired
    private SysBannerService sysBannerService;
    @Autowired
    private GoodsService goodsService;

    //新增
    @PostMapping
    public String add(@RequestBody SysBanner sysBanner){
        if(sysBannerService.save(sysBanner)){
            return "新增成功!";
        }
        throw new CustomException("新增失败!");
    }

    //编辑
    @PutMapping
    public String edit(@RequestBody SysBanner sysBanner){
        if(sysBannerService.updateById(sysBanner)){
            return "编辑成功!";
        }
        throw new CustomException("编辑失败!");
    }

    //删除
    @DeleteMapping("/{banId}")
    public String delete(@PathVariable("banId") Long banId){
        if(sysBannerService.removeById(banId)){
            return "删除成功!";
        }
        throw new CustomException("删除失败!");
    }

    //列表
    @GetMapping("/list")
    public IPage<SysBanner> list(SysBannerParam param){
        //构造分页对象
        IPage<SysBanner> page = new Page<>(param.getCurPage(),param.getPageSize());
        //构造查询条件
        QueryWrapper<SysBanner> query = new QueryWrapper<>();
        query.lambda().like(StrUtil.isNotEmpty(param.getTitle()),SysBanner::getTitle,param.getTitle())
                .orderByAsc(SysBanner::getOrderNum);
        return sysBannerService.page(page, query);
    }

    //商品列表
    @GetMapping("/getGoods")
    public List<SelectType> getGoods(){
        //查询商品
        QueryWrapper<Goods> query = new QueryWrapper<>();
        query.lambda().eq(Goods::getStatus,"0").eq(Goods::getSellStatus,"0");
        List<Goods> list = goodsService.list(query);
        //组装select需要的数据格式
        List<SelectType> selectList = new ArrayList<>();
        for (Goods goods : list) {
            SelectType type = new SelectType();
            type.setValue(goods.getGoodsId());
            type.setLabel(goods.getGoodsName());
            selectList.add(type);
        }
        return selectList;
    }

    //上架下架
    @PostMapping("/upandown")
    public String upandown(@RequestBody SysBanner sysBanner){
        UpdateWrapper<SysBanner> query = new UpdateWrapper<>();
        query.lambda().set(SysBanner::getStatus,sysBanner.getStatus())
                .eq(SysBanner::getBanId,sysBanner.getBanId());
        if(sysBannerService.update(query)){
            return "设置成功!";
        }
        throw new CustomException("编辑失败!");
    }

    @GetMapping("/getIndexBanner")
    public List<SysBanner> getIndexBanner(){
        QueryWrapper<SysBanner> query = new QueryWrapper<>();
        query.lambda().eq(SysBanner::getStatus,"0") //上架状态
                .orderByAsc(SysBanner::getOrderNum);
        List<SysBanner> list = sysBannerService.list(query);
        if(!list.isEmpty()){
        //循环查询商品信息
            for (SysBanner sysBanner : list) {
                Long goodsId = sysBanner.getGoodsId();//商品id
                Goods goods = goodsService.getById(goodsId);//查询商品信息
                sysBanner.setGoods(goods);//设置商品信息
            }
        }
        return list;
    }
}
